#pragma once

#include <iostream>
#include <set>
#include "InetAddr.hpp"
#include "Log.hpp"

using namespace LogModule;

class Command
{
public:
    Command() 
    {
        _white_list_commands.insert("ls");
        _white_list_commands.insert("pwd");
        _white_list_commands.insert("whoami");
        _white_list_commands.insert("touch a.txt");
        _white_list_commands.insert("rm -rf a.txt");
        _white_list_commands.insert("ls -l");
    }

    bool IsSafe(const std::string &command)
    {
        return _white_list_commands.find(command) != _white_list_commands.end();
    }

    std::string Execute(const std::string &command, InetAddr &addr)
    {
        if(!IsSafe(command))
            return "坏人";
        std::string result;
        FILE* pf = popen(command.c_str(), "r");
        if(pf == NULL)
        {
            perror("popen");
            return std::string("没有这个命令：") + command;
        }
        char line[1024];
        while(fgets(line, sizeof(line), pf))
        {
            result += line;
        }
        result = addr.AddrToString() + "->" + line;
        return result;
    }

    ~Command() {}
private:
    std::set<std::string> _white_list_commands;
};